﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Moonlite.Data.Configuration.UI
{
    public class FontComboBox : ComboBox
    {
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (e.Index == -1 || e.Index >= Items.Count)
            {
                return;
            }

            e.DrawBackground();
            if ((e.State & DrawItemState.Focus) != 0)
            {
                e.DrawFocusRectangle();
            }

            Brush b = null;

            try
            {
                b = new SolidBrush(e.ForeColor);
                e.Graphics.DrawString(((Font)Items[e.Index]).Name, ((Font)Items[e.Index]), b, e.Bounds);
            }
            finally
            {
                if (b != null)
                {
                    b.Dispose();
                }

                b = null;
            }
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            CreateSampleFonts();
        }

        private void CreateSampleFonts()
        {
            if (!IsHandleCreated || DesignMode)
            {
                return;
            }

            if (Items.Count > 0)
            {
                for (int x = 0; x < Items.Count; x++)
                {
                    ((Font)Items[x]).Dispose();
                }

                Items.Clear();
            }

            FontFamily[] ff = FontFamily.Families;
            for (int x = 0; x < ff.Length; x++)
            {
                Font font = null;
                if (ff[x].IsStyleAvailable(FontStyle.Regular))
                {
                    font = new System.Drawing.Font(ff[x].Name, Font.Size);
                }
                else if (ff[x].IsStyleAvailable(FontStyle.Bold))
                {
                    font = new System.Drawing.Font(ff[x].Name, Font.Size, FontStyle.Bold);
                }
                else if (ff[x].IsStyleAvailable(FontStyle.Italic))
                {
                    font = new System.Drawing.Font(ff[x].Name, Font.Size, FontStyle.Italic);
                }
                else if (ff[x].IsStyleAvailable(FontStyle.Strikeout))
                {
                    font = new System.Drawing.Font(ff[x].Name, Font.Size, FontStyle.Strikeout);
                }
                else if (ff[x].IsStyleAvailable(FontStyle.Underline))
                {
                    font = new System.Drawing.Font(ff[x].Name, Font.Size, FontStyle.Underline);
                }

                if (font != null)
                {
                    Items.Add(font);
                }
            }
        }

        public FontComboBox()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            DisplayMember = "Name";
            SetStyle(ControlStyles.StandardClick, false);
            SetStyle(ControlStyles.Selectable, false);
            SetStyle(ControlStyles.UserPaint, false);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, false);
        }
    }
}
